package com.ztesoft.web.resourcecheck;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.ztesoft.core.db.ds.DynamicDataSourceManager;
import com.ztesoft.core.spring.context.SpringApplicationContextHolder;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.exception.ExceptionHandler;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.Utils;
import com.ztesoft.iocp.cipher.util.RSAUtils;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.Util;
import com.ztesoft.web.common.vo.HostConnInfoVO;
import com.ztesoft.web.common.vo.HostConnMethodVO;
import com.ztesoft.web.common.vo.HostInfoPOJO;
import com.ztesoft.web.syncbaseinfo.service.BaseInfoService;


/**
 * oracle锁模式查询<br>
 * 
 * @author <a href="mailto:lin.weibin@zte.com.cn">lin.wb</a> 2016年5月4日 上午10:50:21
 * @version 1.0
 */
@Service
public class LModeCheck {

	private static final ZTEsoftLogManager LOGGER = ZTEsoftLogManager.getLogger(LModeCheck.class);
	
	@Autowired
	private DynamicDataSourceManager dynamicDataSourceManager;
	
	@Autowired
	private BaseInfoService baseInfoService;
	
	@Autowired 
	private HostResCheck hostResCheck;
	/**
	 * oracle锁模式查询
	 * @param dbLinkId 数据库实例表主机标志
	 * @param tableName 查询的表名
	 * @param dbConfig 数据库配置信息 ({数据库类型[0]<小写>，数据库连接[1]，用户名[2]，密码[3],数据库名[4]}) 通过baseinfoservice.getDbConfig(...)获取 
	 * @throws BaseAppException 
	 */
	public List<Map<String, Object>> qryOraLMode(Integer dbLinkId, String tableName, String[] dbConfig) throws BaseAppException {
		LOGGER.debug("oracle锁模式查询  ..");
		String qrySql = DMSConstant.ORA_LMODE_SQL;//获取得到sql
		try {//
			DataSource dataSource = 
					dynamicDataSourceManager.getDruidDataSource(dbLinkId+dbConfig[2], dbConfig);
			return new JdbcTemplate(dataSource).queryForList(qrySql, tableName);
		} catch (Exception e) {
			LOGGER.error("oracle锁模式异常",e);
			throw ExceptionHandler.publish("oracle锁模式异常", e);
		}
	}
	
	public String qryIfxLMode(Integer dbLinkId, String tableName, String[] dbConfig) throws BaseAppException {
		if (!dbConfig[0].equals("informix")) {
			throw ExceptionHandler.publish("只允许informix类型数据库执行此操作");
		}
		LOGGER.debug("informix锁模式查询  ..");
		try {			
			HostConnInfoVO hostConnInfoVO = baseInfoService.qryHostConn(dbLinkId);
			List<HostConnMethodVO> hostConnMethodVOList = hostConnInfoVO.getHostConnMethod();
			
			Map<String, HostConnMethodVO> connMethodMap = new HashMap<String, HostConnMethodVO>(hostConnMethodVOList.size());
			for (HostConnMethodVO hostConnMethodVO : hostConnMethodVOList) {
				connMethodMap .put(hostConnMethodVO.getConnMethod().toLowerCase(), hostConnMethodVO);
			}
			HostConnMethodVO hostConnMethodVO = null;
			if (!Utils.isEmpty(connMethodMap.get(DMSConstant.HOST_LINK_SSH))) {
				hostConnMethodVO = connMethodMap.get(DMSConstant.HOST_LINK_SSH);
				hostResCheck.setCheckStrategy(SpringApplicationContextHolder.getBean(SSHCheckStrategy.class));
			} else if (!Utils.isEmpty(connMethodMap.get(DMSConstant.HOST_LINK_TELNET))) {
				hostConnMethodVO = connMethodMap.get(DMSConstant.HOST_LINK_TELNET);
				hostResCheck.setCheckStrategy(SpringApplicationContextHolder.getBean(TelNetCheckStrategy.class));
			}
			HostInfoPOJO hostInfo = new HostInfoPOJO(hostConnInfoVO.getOsType(), hostConnInfoVO.getIp(), hostConnMethodVO.getUserName(), 
					RSAUtils.decryptString(hostConnMethodVO.getPassword()), hostConnMethodVO.getPort(), false);
			String result = hostResCheck.getIfxLMode(hostInfo, dbConfig[4], tableName);
			if (result.contains("Page")) {
				LOGGER.debug("result.contains(\"Page\")");
				result = "Page";
			} else if (result.contains("Row")) {
				LOGGER.debug("result.contains(\"Row\")");
				result = "Row";
			} else {
				result = Util.dealResult(result);
			}
			LOGGER.info("锁模式查询结果{0}",result);
			return result;
		} catch (Exception e) {
			LOGGER.error("informix锁模式异常",e);
			throw ExceptionHandler.publish("informix锁模式异常", e);
		}
	}
	
	
}
